package org.jspar.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.jspar.Constants;
import org.jspar.tile.Rectangle;

public class ModuleModel {

	private String name;
	private int width;
	private int height;
	private Map<String, TerminalModel> terminals = new HashMap<String, TerminalModel>();

	public ModuleModel(String name, int width, int height) {
		this.name = name;
		this.width = width;
		this.height = height;
	}
	public String name() { return name; }
	public int width() { return width; }
	public int height() { return height; }

	public void addTerminal(TerminalModel t) {
		terminals.put(t.name(), t);
	}

	public TerminalModel findTerminal(String name) {
		return terminals.get(name);
	}
	public boolean isOut() {
		return name.equals("OUT");
	}
	public boolean isIn() {
		return name.equals("IN");
	}
	public boolean isInOut() {
		return name.equals("INOUT");
	}
	public boolean isSystemTerminal() {
		return isIn() || isOut() || isInOut();
	}
	public void setDefaultObjectSize(Module mod) {
		mod.setSize(width, height);
	}
	public Collection<TerminalModel> getTerminals() {
		return terminals.values();
	}
	public Rectangle assignTileDimensions(Module m) {
		int xPos, yPos, xLen, yLen;
		if (name.equals("BUFFER") || name.equals("NOT")
				|| name.equals("AND") || name.equals("NAND")  || name.equals("OR")
				|| name.equals("NOR") || name.equals("XOR")  || name.equals("XNOR")) {
			xPos = m.x() - Constants.TERM_MARGIN;
			yPos = m.y();
			xLen = m.width() + 2 * Constants.TERM_MARGIN;
			yLen = m.height();
		} else if (isIn()) {
			xPos = m.x();
			yPos = m.y();
			xLen = m.width() + Constants.TERM_MARGIN;
			yLen = m.height();
		} else if (isOut()) {
			xPos = m.x() - Constants.TERM_MARGIN;
			yPos = m.y();
			xLen = m.width();
			yLen = m.height();
		} else {
			xPos = m.x() - Constants.TERM_MARGIN;
			yPos = m.y() - Constants.TERM_MARGIN;
			xLen = m.width() + 2 * Constants.TERM_MARGIN;
			yLen = m.height() + 2 * Constants.TERM_MARGIN;
		}
		return new Rectangle(xPos, yPos, xLen, yLen);
	}
}
